<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Service\Api\common\LogService;
use Closure;

class ApiRequestLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $startTime = microtime(true);
        $response = $next($request);
        $endTime = microtime(true);
        $costTime = bcsub($endTime, $startTime, 2);
        LogService::createApiLogs($request, $costTime, $response->getStatusCode(), $response->headers->get('Content-Length'));
        return $response;
    }
}
